package com.knife.bbs.action;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.knife.bbs.business.ActiveUser;
import com.knife.bbs.business.BBSConfig;
import com.knife.bbs.business.BBSCoreService;
import com.knife.bbs.business.BBSDirService;
import com.knife.bbs.business.BBSDocService;
import com.knife.bbs.business.BBSRights;
import com.knife.bbs.business.BBSUserService;
import com.knife.bbs.business.CollectionsService;
import com.knife.bbs.business.VoteService;
import com.knife.bbs.domain.Collections;
import com.knife.bbs.object.BBSDir;
import com.knife.bbs.object.BBSDoc;
import com.knife.bbs.object.UserInfo;
import com.knife.util.CommUtil;
import com.knife.util.HtmlUtil;
import com.knife.util.TagUtil;
import com.knife.web.ActionContext;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IPageList;

/**
 * 
 * <p>
 * Title:BBS栏目处理类
 * </p>
 * <p>
 * Description: BBS目录的列表处理Action<br>
 * 通过继承com.knife.web.tools.AbstractCmdAction,实现简单的命令式Action处理,去除了复杂烦锁的if else语句
 * </p>
 */
public class BBSDocAction extends BBSAction {
	private BBSDirService dirService = BBSDirService.getInstance();
	private BBSUserService userService = BBSUserService.getInstance();
	private BBSCoreService coreService = BBSCoreService.getInstance();
	private BBSDocService docService = BBSDocService.getInstance();
	private CollectionsService collService=CollectionsService.getInstance();
	private VoteService voteService = new VoteService();

	/**
	 * 在没有跟任何参数的时候执行,默认显示栏目下的所有帖子信息
	 */
	public Page doInit(WebForm form, Module module) {
		// TODO Auto-generated method stub
		return doQuery(form, module);
	}

	/**
	 * 发表新帖子
	 * 
	 * @param form
	 * @param module
	 * @return 若用户有权限发帖子,则返回发帖页面
	 */
	public Page doNew(WebForm form, Module module) {
		String sn = CommUtil.null2String(form.get("sn"));
		ActiveUser user = getCurrentUser();
		BBSDir dir = dirService.getBBSDirBySN(sn);
		if (user == null || user.getPassword().equals(" ")) {
			form.addResult("msg", "您没有登录，不能发表信息！");
			return new Page("login", "login.htm", "html","/"+skin+"/");
		}
		if (dir != null) {
			form.addResult("dir", dir);
		} else {
			form.addResult("msg", "目录不存在，可能是地址链接错误！");
		}
		UserInfo u = userService.getUserInfoByName(user.getUsername());
		if (coreService.noRandomCode(u)){
			form.addResult("need_verify", "true");
		}
		if (coreService.canUploadImg(u)){
			form.addResult("canUpload", "true");
		}
		form.addResult("user", user);
		return module.findPage("/"+skin+"/","edit");
	}

	/**
	 * 查询帖子
	 * 
	 * @param form
	 * @param module
	 * @return 返回帖子列表
	 */
	public Page doList(WebForm form, Module module) {
		return doQuery(form, module);
	}

	/**
	 * 保存新帖子信息
	 * 
	 * @param form
	 * @param module
	 * @return 返回列表Page或其它
	 */
	public Page doAdd(WebForm form, Module module) {
		BBSDoc obj = (BBSDoc) form.toPo(BBSDoc.class);
		ActiveUser user = getCurrentUser();
		UserInfo u = user == null ? null : userService.getUserInfoByName(user
				.getUsername());
		if (!coreService.noRandomCode(u)) {
			String randomCode = (String) ActionContext.getContext()
					.getSession().getAttribute("rand");
			if (!randomCode.equals((String) form.get("randomCode"))) {
				form.addResult("msg", "验证码不正确，请重新输入！");
				return new Page("error", "error.htm", "html","/"+skin+"/");
			}
			form.addResult("need_verify", "true");
		}
		if (coreService.canUploadImg(u))
			form.addResult("canUpload", "true");
		// 在Action中检测权限
		// 以下执行表单对象VO到PO转换的代码,通过使用EasyJWeb验证框架中可以省略
		if (!BBSRights.checkRights(obj, "add", user))
			return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
		String quotId = CommUtil.null2String(form.get("quotId"));
		if (!quotId.equals(""))// 处理引用回复
		{
			BBSDoc quot = docService.getBBSDoc(quotId);
			if (quot != null) {
				String QuotoSs = (quot.getContent().length() > 50 ? quot
						.getContent().substring(0, 50) + "..." : quot
						.getContent());
				String QuotoS = "[QUOTE]" + QuotoSs + "[/QUOTE]";
				obj.setContent(QuotoS + obj.getContent());
			}
		}
		if (user == null) {// guest发表
			user = new ActiveUser();
			user.setUsername("guest");
			user.setIp(ActionContext.getContext().getRequest().getRemoteAddr());
		}
		com.knife.bbs.domain.BBSDoc doc=new com.knife.bbs.domain.BBSDoc();
		obj.setCid(doc.getCid());
		//System.out.println("设置好了cid:"+obj.getCid());
		doc.setDoc(obj);
		boolean op = (obj.getParentid() == null || obj.getParentid().equals("")) ? coreService
				.publishMessage(user, obj) : coreService.replyMessage(user, obj);
		if (op) {
			form.addResult("msg", "信息发布成功！");
		} else {
			form.addResult("msg", "信息发布失败！");
		}
		// form.getTextElement().put("sn",obj.getDirSn());
		// return doQuery(form,module);
		/*
		 * 不登录也能发帖子 } else { form.addResult("msg","您没有登录，不能发表文章！"); return new
		 * Page("login","/bbsuser.do","template"); }
		 */
		if (obj.getParentid() == null || obj.getParentid().equals("")){
			//return new Page("bbsList", obj.getHref(), "url");
		} else {
			BBSDoc parent = docService.getBBSDoc(obj.getParentid());
			//return new Page("bbsList", parent.htmlUrl(), "url");
		}
		return doList(form, module);
	}

	public Page doRss(WebForm form, Module module) {
		form.addResult("hostname", BBSConfig.getInstance().getHost_name());
		form.addResult("list", coreService.getWeekMessages());
		ActionContext.getContext().getResponse().setContentType("text/xml");
		ActionContext.getContext().getResponse().setCharacterEncoding("utf-8");
		try {
			ActionContext.getContext().getResponse().flushBuffer();
		} catch (Exception e) {

		}
		return module.findPage("/"+skin+"/","rss");
	}

	/**
	 * 修改帖子信息
	 * 
	 * @param form
	 * @param module
	 * @return 返回帖子列表Page
	 */
	public Page doUpdate(WebForm form, Module module) {
		String cid = CommUtil.null2String(form.get("cid"));
		ActiveUser user = getCurrentUser();
		BBSDoc obj = docService.getBBSDoc(cid);
		UserInfo u = userService.getUserInfoByName(user.getUserName());
		if (!coreService.noRandomCode(u)) {
			String randomCode = (String) ActionContext.getContext()
					.getSession().getAttribute("rand");
			if (!randomCode.equals((String) form.get("randomCode"))) {
				form.addResult("msg", "验证码不正确，请重新输入！");
				return new Page("error", "error.htm", "html","/"+skin+"/");
			}
			form.addResult("need_verify", "true");
		}
		if (coreService.canUploadImg(u))
			form.addResult("canUpload", "true");
		if (user != null) {
			if (obj != null) {
				// 在Action中检测权限
				// 以下执行表单对象VO到PO转换的代码,通过使用EasyJWeb验证框架中可以省略
				if (!BBSRights.checkRights(obj, "update", user))
					return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
				form.toPo(obj);
				coreService.updateMessage(user, obj);
			} else {
				form.addResult("msg", "所要修改的目录不存在！");
			}
		} else {
			form.addResult("msg", "您没有登录，不能发表文章！");
			return new Page("login", contextPath+"/bbsuser.do", "url");
		}
		return new Page("bbsList", contextPath+"/bbsdoc.do?parameter=list&&sn="
				+ obj.getDirsn(), "url");
	}

	public Page doGetRoleJS(WebForm form, Module module) throws Exception {
		ActiveUser user = getCurrentUser();
		String cid = CommUtil.null2String(form.get("cid"));
		BBSDoc doc = docService.getBBSDoc(cid);
		if (doc != null) {
			doc.setReadtimes(new Integer(doc.getReadtimes().intValue() + 1));
			docService.update(doc);
		}
		HttpServletResponse response = ActionContext.getContext().getResponse();
		boolean need_verify = true, canUploadImg = false;
		if (user != null) {
			UserInfo u = userService.getUserInfoByName(user.getUserName());
			if (coreService.noRandomCode(u))
				need_verify = false;
			;
			if (coreService.canUploadImg(u))
				canUploadImg = true;
		}
		response.setContentType("text/plain;charset=utf-8");
		response.getWriter().write(
				need_verify ? "var need_verify=1;" : "var need_verify=0;");
		response.getWriter().write("\n");
		response.getWriter().write(
				canUploadImg ? "var canUploadImg=1;" : "var canUploadImg=0;");
		response.getWriter().write("\n");
		response.getWriter().write(
				user != null ? "var myUserName='" + user.getUserName() + "';"
						: "var myUserName='';\n");
		response.getWriter().flush();
		response.getWriter().close();
		return null;
	}

	/**
	 * 显示帖子内容
	 * 
	 * @param form
	 * @param module
	 * @return 返回显示帖子的Page
	 */
	public Page doShow(WebForm form, Module module) throws Exception {
		String cid = CommUtil.null2String(form.get("cid"));
		int currentPage = CommUtil.null2Int(form.get("page"));
		int pageSize = CommUtil.null2Int(form.get("pageSize"));
		ActiveUser user = getCurrentUser();
		BBSDoc obj = docService.getBBSDoc(cid);
		if (obj != null) {
			if (obj.isReply()) {
				BBSDoc pObj = docService.getBBSDoc(obj.getParentid());
				String url = pObj.htmlUrl();
				if (!url.endsWith("htm")){
					url += "&somethine";
				}
				return new Page("bbsShow", url + "#" + obj.getCid(), "url");
			}
			if (currentPage < 1)
				currentPage = 1;
			if (pageSize < 1)
				pageSize = 15;
			BBSDir dir = dirService.getBBSDirBySN(obj.getDirsn());
			if (dir != null) {
				form.addResult("dir", dir);
			} else {
				form.addResult("msg", "目录不存在，可能是地址链接错误！");
			}
			if (user != null) {
				UserInfo u = userService.getUserInfoByName(user.getUsername());
				if (coreService.noRandomCode(u))
					form.addResult("need_verify", "true");
				if (coreService.canUploadImg(u))
					form.addResult("canUpload", "true");
				//form.addResult("user", user);
			}
			// 保存点击数
			obj.setReadtimes(new Integer(obj.getReadtimes() != null ? obj
					.getReadtimes().intValue() + 1 : 1));
			com.knife.bbs.domain.BBSDoc doc=new com.knife.bbs.domain.BBSDoc();
			doc.setDoc(obj);
			docService.update(doc);
			// 显示并处理主题信息
			// 以下执行表单对象PO到VO转换的代码,大部分在使用EasyJWeb验证框架中可以省略
			// form.addPo(obj);
			String content;
			if (BBSRights.checkRights(obj, "read", user))
				content = HtmlUtil
						.getUBB2HTML(coreService.replaceEmotTag(HtmlUtil
								.escapeHTMLTag(obj.getContent())));
			else
				content = obj.showVRTypeTitle();
			obj.setContent(content);
			/*
			form.addResult("cid", obj.getCid());
			form.addResult("title", obj.getTitle());
			form.addResult("content", content);
			form.addResult("inputtime", obj.getInputtime());
			form.addResult("modifytime", obj.getModifytime());
			if (obj.getTagpic() != null && (!obj.getTagpic().equals("")))
				form.addResult("tagpic", obj.getTagpic());
			if (obj.getImgs() != null && (!obj.getImgs().equals("")))
				form.addResult("imgs", obj.getImgs());
			form.addResult("files", obj.getFiles());
			form.addResult("ip", obj.getIp());
			*/
			form.addResult("doc", obj);
			if (user != null) {
				if (BBSRights.checkRights(obj, "setTopMessage", user))// 置顶
					form.addResult("topMessage", "true");
				if (BBSRights.checkRights(obj, "setEliteMessage", user))// 精华
					form.addResult("eliteMessage", "true");
				if (BBSRights.checkRights(obj, "lockMessage", user))// 锁定
					form.addResult("lockMessage", "true");
				if (BBSRights.checkRights(obj, "editMessage", user))// 修改
					form.addResult("editMessage", "true");
				if (BBSRights.checkRights(obj, "delMessage", user))// 删帖
					form.addResult("delMessage", "true");
				if (BBSRights.checkRights(obj, "moveMessage", user))// 移动
					form.addResult("moveMessage", "true");
				if (BBSRights.checkRights(obj, "lockUser", user))// 锁定用户
					form.addResult("lockUser", "true");
			}
			if (obj.isLock())
				form.addResult("isLock", "true");
				form.addResult("username", obj.getInputuser());
				form.addResult("encodUserName",
					URLEncoder.encode(obj.getInputuser(), "utf-8"));
			// 取得发帖人的相关信息
			 UserInfo inputUser=userService.getUserInfoByName(obj.getInputuser());
			 if(inputUser!=null) {
				 form.addResult("topicUser",inputUser);
			 }
			// 处理回复信息
			IPageList pList = coreService
					.queryReply(obj, pageSize, currentPage);
			List replyList = new ArrayList();
			if (pList != null) {
				List list = pList.getResult();
				if (list != null) {
					for (int i = 0; i < list.size(); i++) {
						Map map = new HashMap();
						com.knife.bbs.domain.BBSDoc reply = (com.knife.bbs.domain.BBSDoc) list.get(i);
						BBSDoc replyDoc=new BBSDoc(reply);
						String rcontent=HtmlUtil.getUBB2HTML(coreService
								.replaceEmotTag(HtmlUtil.escapeHTMLTag(reply
										.getContent())));
						replyDoc.setContent(rcontent);
						map.put("doc",replyDoc);
						/*
						map.put("cid", reply.getCid());
						map.put("title", reply.getTitle());
						map.put("content", HtmlUtil.getUBB2HTML(coreService
								.replaceEmotTag(HtmlUtil.escapeHTMLTag(reply
										.getContent()))));
						map.put("inputtime", reply.getInputtime());
						map.put("modifytime", reply.getModifytime());
						map.put("ip", reply.getIp());
						map.put("imgs", reply.getImgs() != null ? (reply
								.getImgs().equals("") ? null : reply.getImgs())
								: null);
						map.put("files", reply.getFiles());
						if (reply.getTagpic() != null
								&& (!reply.getTagpic().equals("")))
							map.put("tagpic", reply.getTagpic());
						*/
						map.put("floor", new Integer((pageSize * (currentPage - 1) + i + 1)));
						// 处理用户帖子管理权限
						if (user != null) {
							if (BBSRights.checkRights(replyDoc,"lockMessage",user))
								map.put("lockMessage", "true");
							if (BBSRights.checkRights(replyDoc,"editMessage",user))
								map.put("editMessage", "true");
							if (BBSRights.checkRights(replyDoc,"delMessage",user))
								map.put("delMessage", "true");
							if (BBSRights.checkRights(replyDoc,"lockUser",user))
								map.put("lockUser", "true");
						}
						map.put("username", reply.getInputuser());
						map.put("encodUserName", URLEncoder.encode(
								reply.getInputuser(), "utf-8"));
						UserInfo replyUser=userService.getUserInfoByName(obj.getInputuser());
						if(inputUser!=null) {
							map.put("replyUser",replyUser);
						}
						replyList.add(map);
					}
				}
				form.addResult("list", replyList);
				form.addResult("pages", new Integer(pList.getPages()));
				form.addResult("rows", new Integer(pList.getRowCount()));
				form.addResult("gotoPageHTML",TagUtil.showPageHtml(pList.getCurrentPage(), pList.getPages()));
				form.addResult("page", new Integer(currentPage));
			}
		}
		return module.findPage("/"+skin+"/","show");
	}

	/**
	 * 编辑帖子-从持久层读取数据到VO中显示
	 * 
	 * @param form
	 * @param module
	 * @return 帖子编辑页面
	 */
	public Page doEdit(WebForm form, Module module) {
		String cid = CommUtil.null2String(form.get("cid"));
		ActiveUser user = getCurrentUser();
		BBSDoc obj = docService.getBBSDoc(cid);
		if (obj != null) {
			if (!BBSRights.checkRights(obj, "edit", user))
				return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
			BBSDir dir = dirService.getBBSDirBySN(obj.getDirsn());
			if (dir != null) {
				form.addResult("dir", dir);
			} else {
				form.addResult("msg", "目录不存在，可能是地址链接错误！");
			}
			if (obj.getTagpic() != null && obj.getTagpic().equals(""))
				obj.setTagpic(null);
			form.addPo(obj);
		}
		UserInfo u = userService.getUserInfoByName(getCurrentUser().getUsername());
		if (coreService.noRandomCode(u))
			form.addResult("need_verify", "true");
		if (coreService.canUploadImg(u))
			form.addResult("canUpload", "true");
		form.addResult("user", user);
		return module.findPage("/"+skin+"/","edit");
	}

	public Page doShowMove(WebForm form, Module module) {
		// String dirsn=CommUtil.null2String(form.get("dirsn"));
		List list = dirService
				.queryBBSDir("status>=0 order by parentSn,sequence");
		form.addResult("list", list);
		return module.findPage("/"+skin+"/","showMove");
	}

	public Page doMoveMessage(WebForm form, Module module) throws Exception {
		String cid = CommUtil.null2String(form.get("cid"));
		ActiveUser user = getCurrentUser();
		BBSDoc obj = docService.getBBSDoc(cid);
		if (obj != null) {
			if (!BBSRights.checkRights(obj, "moveMessage", user))
				return new Page("popedomError", "norights.htm", "page","/"+skin+"/");
			BBSDir dir = dirService.getBBSDirBySN((String) form.get("dirsn"));
			if (dir != null) {
				obj.setDirsn(dir.getSn());
				obj.setDirpath(dir.getDirpath());
				com.knife.bbs.domain.BBSDoc doc=new com.knife.bbs.domain.BBSDoc();
				doc.setDoc(obj);
				docService.update(doc);
				form.addResult("msg", "移动成功！");
			} else {
				form.addResult("msg", "移动失败!");
			}
		}
		return doShow(form, module);
	}

	/**
	 * 置顶操作,把帖子置顶或者取消置顶
	 * 
	 * @param form
	 * @param module
	 * @return 返回帖子列表显示Page
	 */
	public Page doSetTopMessage(WebForm form, Module module) {
		String cid = CommUtil.null2String(form.get("cid"));
		ActiveUser user = getCurrentUser();
		BBSDoc obj = docService.getBBSDoc(cid);
		if (obj != null) {
			if (!BBSRights.checkRights(obj, "setTopMessage", user))
				return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
			obj.setTopmessage(new Integer(obj.getTopmessage() != null
					&& (obj.getTopmessage().intValue() == 1) ? 0 : 1));
			com.knife.bbs.domain.BBSDoc adoc=new com.knife.bbs.domain.BBSDoc();
			adoc.setDoc(obj);
			docService.update(adoc);
			form.addResult("msg", "操作成功！");
		}
		return doQuery(form, module);
	}

	/**
	 * 精华帖子操作,把帖子标记为精华或者取消精华
	 * 
	 * @param form
	 * @param module
	 * @return 返回帖子列表显示Page
	 */
	public Page doSetEliteMessage(WebForm form, Module module) {
		String cid = CommUtil.null2String(form.get("cid"));
		ActiveUser user = getCurrentUser();
		BBSDoc obj = docService.getBBSDoc(cid);
		if (obj != null) {
			if (!BBSRights.checkRights(obj, "setEliteMessage", user))
				return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
			obj.setElitemessage(new Integer(obj.getElitemessage() != null
					&& (obj.getElitemessage().intValue() == 1) ? 0 : 1));
			com.knife.bbs.domain.BBSDoc doc=new com.knife.bbs.domain.BBSDoc();
			doc.setDoc(obj);
			docService.update(doc);
			form.addResult("msg", "操作成功！");
		}
		return doQuery(form, module);
	}

	/**
	 * 锁定帖子
	 * @param form
	 * @param module
	 * @return
	 */
	public Page doLockMessage(WebForm form, Module module) {
		String cid = CommUtil.null2String(form.get("cid"));
		ActiveUser user = getCurrentUser();
		BBSDoc obj = docService.getBBSDoc(cid);
		if (obj != null) {
			if (!BBSRights.checkRights(obj, "setEliteMessage", user))
				return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
			boolean ret = coreService.lockMessage(user, obj);
			form.addResult("msg", ret ? "操作成功！" : "操作失败!");
		}
		return doQuery(form, module);
	}
	
	/**
	 * 收藏帖子
	 * @param form
	 * @param module
	 * @return
	 */
	public Page doFavMessage(WebForm form, Module module) {
		String cid = CommUtil.null2String(form.get("cid"));
		ActiveUser user = getCurrentUser();
		BBSDoc obj = docService.getBBSDoc(cid);
		if (obj != null) {
			if (!BBSRights.checkRights(obj, "setEliteMessage", user)){
				return new Page("popedomError", "norights.htm", "html","/"+skin+"/");
			}
			Collections ac=new Collections();
			ac.setUsername(user.getUsername());
			ac.setUrl(contextPath+"/bbsdoc.do?parameter=show&cid="+cid);
			boolean ret = collService.add(ac);
			form.addResult("msg", ret ? "操作成功！" : "操作失败!");
		}
		return doQuery(form, module);
	}

	/**
	 * 删除帖子
	 * 
	 * @param form
	 * @param module
	 * @return 返回帖子列表Page
	 */
	public Page doDel(WebForm form, Module module) {
		String cid = CommUtil.null2String(form.get("cid"));
		ActiveUser user = getCurrentUser();
		BBSDoc obj = docService.getBBSDoc(cid);
		if (obj != null) {
			if (!BBSRights.checkRights(obj, "del", user))
				return new Page("popedomError", "norights.htm", "page","/"+skin+"/");
			com.knife.bbs.domain.BBSDoc doc=new com.knife.bbs.domain.BBSDoc();
			doc.setDoc(obj);
			docService.del(doc);
			form.addResult("msg", "删除成功！");
		}
		return doQuery(form, module);
	}

	/**
	 * 最后更新的帖子-把最新的精华的帖子从特久层读出并到View层显示
	 * 
	 * @param form
	 * @param module
	 * @return 帖子列表Page
	 */
	public Page doLastElite(WebForm form, Module module) {
		int currentPage = CommUtil.null2Int(form.get("page"));
		int pageSize = CommUtil.null2Int(form.get("pageSize"));
		String sn = CommUtil.null2String(form.get("sn"));
		if (sn.equals(""))
			sn = CommUtil.null2String(form.get("dirsn"));
		BBSDir dir = dirService.getBBSDirBySN(sn);
		if (dir == null) {
			com.knife.bbs.domain.BBSDir adir=new com.knife.bbs.domain.BBSDir();
			dir = new BBSDir(adir);
			dir.setTitle("最新精华");
			dir.setIntro("论坛中的所有精华帖！");
		}
		if (currentPage < 1)
			currentPage = 1;
		if (pageSize < 1)
			pageSize = 15;
		String sqlWhere = "eliteMessage>0 ";
		Collection paras = new ArrayList();
		if (dir.getSn() != null && (!"".equals(dir.getSn()))) {
			sqlWhere += " and dirsn=?";
			paras.add(sn);
		}
		IPageList pList = coreService.queryMessage(sqlWhere, paras, pageSize,
				currentPage);
		if (pList != null) {
			//com.knife.util.CommUtil.saveIPageList2WebForm(pList, form);
			CommUtil.saveIPageList2WebForm(pList, form,BBSDoc.class.getName(),com.knife.bbs.domain.BBSDoc.class);
		}
		return module.findPage("/"+skin+"/","list");
	}

	public Page doMyList(WebForm form, Module module) throws Exception {
		String inputUser = URLDecoder.decode(
				URLEncoder.encode((String) form.get("user"), "ISO8859_1"),
				"utf-8");
		int currentPage = CommUtil.null2Int(form.get("page"));// 页码
		int pageSize = CommUtil.null2Int(form.get("pageSize"));// 每页大小
		String sn = CommUtil.null2String(form.get("sn"));// 目录编号
		String title = CommUtil.null2String(form.get("title"));// 标题
		String topic = CommUtil.null2String(form.get("topic"));// 标题
		if (sn.equals(""))
			sn = CommUtil.null2String(form.get("dirsn"));// 目录编码
		if ("".equals(inputUser))
			inputUser = CommUtil.null2String(form.get("inputuser"));
		if ("".equals(inputUser) && (this.getCurrentUser() != null))
			inputUser = this.getCurrentUser().getUserName(); // 录入人
		if (currentPage < 1)
			currentPage = 1;
		if (pageSize < 1)
			pageSize = 15;
		Collection paras = new ArrayList();
		String sqlWhere = " inputUser= ?";
		paras.add(inputUser);
		if (!title.equals("")) {
			sqlWhere += " and title like ?";
			paras.add("%" + title + "%");
		}
		if (!"".equals(topic)) {
			sqlWhere += " and (parentId is null or parentId='')";
		}
		IPageList pList = coreService.queryBBSDoc(sqlWhere, paras, pageSize,
				currentPage);
		if (pList != null) {
			com.knife.util.CommUtil.saveIPageList2WebForm(pList, form,BBSDoc.class.getName(),com.knife.bbs.domain.BBSDoc.class);
		}
		form.addResult("inputuser", inputUser);
		return module.findPage("/"+skin+"/","myList");
	}

	public Page doQuery(WebForm form, Module module) {
		int currentPage = CommUtil.null2Int(form.get("page"));// 页码
		int pageSize = CommUtil.null2Int(form.get("pageSize"));// 每页大小
		String sn = CommUtil.null2String(form.get("sn"));// 目录编号
		String title = CommUtil.null2String(form.get("s_title"));// 标题
		if (sn.equals("")){
			sn = CommUtil.null2String(form.get("dirsn"));// 目录编码
		}
		String inputUser = CommUtil.null2String(form.get("s_inputuser")); // 录入人
		BBSDir dir = dirService.getBBSDirBySN(sn);
		if (dir != null) {
			if (currentPage < 1)
				currentPage = 1;
			if (pageSize < 1)
				pageSize = 15;
			Collection<Object> paras = new ArrayList<Object>();
			String sqlWhere = " dirsn=?";
			paras.add(sn);
			if (!title.equals("")) {
				sqlWhere += " and title like ?";
				paras.add("%" + title + "%");
				form.addResult("s_title", title);
			}
			if (!inputUser.equals("")) {
				sqlWhere += " and inputUser=?";
				paras.add(inputUser);
				form.addResult("s_inputuser", inputUser);
			}
			IPageList pList = coreService.queryMessage(sqlWhere, paras,
					pageSize, currentPage);
			form.addResult("dir", dir);
			if (pList != null) {
				CommUtil.saveIPageList2WebForm(pList, form,BBSDoc.class.getName(),com.knife.bbs.domain.BBSDoc.class);
			}
		} else {
			form.addResult("msg", "所查询的目录不存在！");
		}
		return module.findPage("/"+skin+"/","list");
	}

	/**
	 * 通过Session获得当前登录用户
	 * 
	 * @return 当前操作用户
	 */
	private ActiveUser getCurrentUser() {
		ActiveUser user = (ActiveUser) ActionContext.getContext().getSession()
				.getAttribute("bbsuser");
		return user;
	}

	public Object doAfter(WebForm form, Module module) {
		form.addResult("dirService", dirService);
		form.addResult("docService", docService);
		form.addResult("coreService", coreService);
		return super.doAfter(form, module);
	}

}
